# Maintainer: David Runge <dvzrv@archlinux.org>

_name=IEMPluginSuite
pkgbase=iempluginsuite
pkgname=(
  iempluginsuite
  iempluginsuite-{standalone,vst3}
)
pkgver=1.14.1
pkgrel=2
pkgdesc="Plug-in suite including Ambisonic plug-ins up to 7th order"
arch=(loong64 x86_64)
url="https://plugins.iem.at"
license=(GPL3)
makedepends=(
  alsa-lib
  cmake
  fftw
  freetype2
  gcc-libs
  gendesk
  glibc
  jack
  juce
  libglvnd
  libjpeg-turbo
  libpng
  libx11
  libxcursor
  libxext
  libxinerama
  libxrandr
  zlib
)
source=(
  $pkgbase-$pkgver.tar.gz::https://git.iem.at/audioplugins/$_name/-/archive/v$pkgver/$_name-v$pkgver.tar.gz
  $pkgbase.directory
  $pkgbase.menu
)
sha512sums=('4eafbdb22470b9bd1d15bf9da799801c9a18e6a06a3b8b50c4a4f2c8410099851e25bd819eb5fdecfa22ac1617c3448b6641fdf9d66a582a78265fa09d93c829'
            '412ac2581089babfe57b10c06dca86c19a8b708b76af70283d3b35dd1deffc8f7c654d9e7d182da8b0fa2f40cafe46fc855890e0aed7d11530de3c0d56bf17dd'
            '2d290b5b8c9320013abb97e0a9051c6687a92700433454c88d4c89fe122c9b1537e128faf7060715c63eb28e93ad9f3f1a98765a7a5934e4f8bd0a8c20254ab3')
b2sums=('29fb3570087a68a63e19df5381a8782b1385b04b3b716e9d81864d6d37cf34a7ad88440a2d2d6c68bc49ee3fdb294e163b628bc9edc4962deb7442e9ff311034'
        'fa85f3ced5143524fac1d6403179052a520307e880902bddd0d239e580c6c2fe1368f6f78bad2950e2e06217e1fd318a52ef810b65e0870636006d87615c7e99'
        '1ea251abb358de7675012b9c9bc6a7187c4c171267f4466437e1d324c1a19ed6e8f5b2eeb625093cfa0303ea534f9d4cd900966338b18126a09a41b6a3939dfc')
_names=(
  AllRADecoder
  BinauralDecoder
  CoordinateConverter
  DirectionalCompressor
  DirectivityShaper
  DistanceCompensator
  DualDelay
  EnergyVisualizer
  FdnReverb
  GranularEncoder
  MatrixMultiplier
  MultiBandCompressor
  MultiEQ
  MultiEncoder
  OmniCompressor
  ProbeDecoder
  RoomEncoder
  SceneRotator
  SimpleDecoder
  StereoEncoder
  ToolBox
)

prepare() {
  local name

  declare -A comments=(
    ['AllRADecoder']="Design an Ambisonic decoder for an arbitrary loudspeaker layout using the AllRAD approach."
    ['BinauralDecoder']="Renders the Ambisonic input signal to a binaural headphone signal using the MagLS approach."
    ['CoordinateConverter']="Converts VST parameters from a spherical representation to a cartesian, and vice versa."
    ['DirectionalCompressor']="A more sophisticated Ambisonic compressor/limiter which lets you control the dynamics for different spatial regions."
    ['DirectivityShaper']="Filter input signal into four independent bands, to which different directivity patterns can be applied to."
    ['DistanceCompensator']="Calculate the needed delays and gains in order to compensate for distance differences."
    ['DualDelay']="Two delay-lines, which can be configured independently."
    ['EnergyVisualizer']="Visualizes the energy distribution on the sphere of the Ambisonic input signal using a Hammer-Aitoff projection (a spherical projection which is area-preserving)"
    ['FdnReverb']="A Feedback-Delay-Network reverberation."
    ['GranularEncoder']="An Ambisonic granular synthesis plug-in."
    ['MatrixMultiplier']="Load a configuration which contains a TransformationMatrix object which will be applied to the input signal."
    ['MultiBandCompressor']="Splits an Ambisonic signal into four bands and compresses them individually."
    ['MultiEQ']="A simple multi-channel equalizer, filtering up to 64 audio channels."
    ['MultiEncoder']="Encode multiple sources with just one plug-in."
    ['OmniCompressor']="An Ambisonic compressor which can also be used as a limiter."
    ['ProbeDecoder']="A little tool to sample/decode the Ambisonic input signal for one specific direction and listen to the output."
    ['RoomEncoder']="Puts a source and a listener into a virtual shoebox-shaped room and renders over 200 wall reflections."
    ['SceneRotator']="An Ambisonic rotator plug-in, which rotates the Ambisonic scene."
    ['SimpleDecoder']="Reads JSON configuration files and decodes the Ambisonic input signal to loudspeaker signals."
    ['StereoEncoder']="Encodes mono or stereo audio signals into the Ambisonic domain."
    ['ToolBox']="Flips the Ambisonic input signal along the x, y and/or z axis."
  )
  declare -A generic=(
    ['AllRADecoder']="Ambisonic Decoder Designer"
    ['BinauralDecoder']="Binaural Decoder"
    ['CoordinateConverter']="VST Coordinate Converter"
    ['DirectionalCompressor']="Compressor Limiter"
    ['DirectivityShaper']="Directivity Shaper"
    ['DistanceCompensator']="Distance Compensation"
    ['DualDelay']="Delay"
    ['EnergyVisualizer']="Audio Visualizer"
    ['FdnReverb']="Reverb"
    ['GranularEncoder']="Granular Synthesis"
    ['MatrixMultiplier']="Matrix Transformation"
    ['MultiBandCompressor']="Multiband Compressor"
    ['MultiEQ']="Multichannel Equalizer"
    ['MultiEncoder']="Ambisonics Encoder"
    ['OmniCompressor']="Ambisonic Limiter Compressor"
    ['ProbeDecoder']="Ambisonic Decoder"
    ['RoomEncoder']="Room Encoder"
    ['SceneRotator']="Ambisonic Scene Rotator"
    ['SimpleDecoder']="Ambisonic Decoder"
    ['StereoEncoder']="Ambisonic Encoder"
    ['ToolBox']="Ambisonic Transformation"
  )

  # juce upstream YOLO moved the directory somewhere between 7.0.5 and 7.0.7
  sed 's|juce_audio_plugin_client/utility|juce_audio_plugin_client/detail|g' -i $_name-v$pkgver/resources/Standalone/MyStandaloneFilterWindow.h

  for name in "${_names[@]}"; do
    gendesk -n \
            --pkgname at.iem.plugins.$name \
            --name $name \
            --exec $name \
            --icon $pkgbase \
            --pkgdesc "${comments[$name]}" \
            --genericname "${generic[$name]}" \
            --categories $pkgbase
  done
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D IEM_BUILD_STANDALONE=ON
    -D IEM_BUILD_VST3=ON
    -D IEM_USE_SYSTEM_JUCE=ON
    -S $_name-v$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

package_iempluginsuite() {
  depends=(
    $pkgbase-{standalone,vst3}
  )

  install -vDm 644 $_name-v$pkgver/{ChangeLog,README}.md -t "$pkgdir/usr/share/doc/$pkgbase/"
}

package_iempluginsuite-standalone() {
  pkgdesc+=" - standalone"
  groups=(
    pro-audio
  )

  local name

  depends+=(
    alsa-lib
    fftw
    freetype2
    gcc-libs
    glibc
    jack  # likely dlopen'ed :(
    libglvnd
    libpng
    libjpeg-turbo
    zlib
  )

  for name in "${_names[@]}"; do
    install -vDm 755 build/$name/${name}_artefacts/None/Standalone/$name -t "$pkgdir/usr/bin/"
  done

  install -vDm 644 ./*.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 $pkgbase.menu -t "$pkgdir/etc/xdg/menus/applications-merged/"
  install -vDm 644 $pkgbase.directory -t "$pkgdir/usr/share/desktop-directories/"
}

package_iempluginsuite-vst3() {
  pkgdesc+=" - VST3 plugins"
  groups=(
    pro-audio
    vst3-plugins
  )

  local name

  depends+=(
    alsa-lib
    fftw
    freetype2
    gcc-libs
    glibc
    libglvnd
    libpng
    libjpeg-turbo
    vst3-host
    zlib
  )

  for name in "${_names[@]}"; do
    install -vDm 755 build/$name/${name}_artefacts/None/VST3/$name.vst3/Contents/$CARCH-linux/$name.so -t "$pkgdir/usr/lib/vst3/$name.vst3/Contents/$CARCH-linux/"
  done
}
